# -*- coding: utf-8 -*-  
from socket import *
from chessAI import *

HOST = '172.16.43.33'
PORT = 4321
ADDR = (HOST, PORT)
ss = socket(AF_INET, SOCK_STREAM, 0)
ss.bind(ADDR)
ss.listen(10)#监听连接个数
AI = ChessAI(CHESS_LEN)

while 1:
	#print('等待客户机连接......\n')
	cs, caddr = ss.accept()
	#print('连接的客户机来自于：', caddr)
	msg = cs.recv(1024).decode()
	data = msg.split('。')
	map_map = []
	x=""
	y=""
	if data[-1]=='0':
		for i in range(0, len(data)-2):
			temp=data[i].replace("[","").replace("]","").replace(",","").replace(" ","")
			map_map.append([int(j) for j in temp])
		x, y= AI.findBestChess(map_map, data[-2])
	else:
		for i in range(0, len(data)-2):
			temp=data[i].replace("[","").replace("]","").replace(",","").replace(" ","")
			map_map.append([int(j) for j in temp])
		x=AI.isWin(map_map, data[-2])
		y='0'
	strs = str(x)+','+str(y)
	cs.sendall(bytes(strs, 'UTF-8'))
	cs.close()
 
ss.close()
